package cn.deali.homework9.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import cn.deali.homework9.MainApp;
import cn.deali.homework9.R;

import cn.deali.homework9.activity.NowPlayingActivity;
import cn.deali.homework9.model.Music;
import cn.deali.homework9.model.OnlineMusic;


public class OnlineMusicAdapter extends RecyclerView.Adapter {
    private static final String TAG = "OnlineMusicAdapter";
    String topMusicListName;
    List<OnlineMusic> onlineMusics;
    Context context;

    public OnlineMusicAdapter(Context context, List<OnlineMusic> onlineMusics, String topMusicListName) {
        this.context = context;
        this.onlineMusics = onlineMusics;
        this.topMusicListName = topMusicListName;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.online_music_item, null);

        //返回ViewHolder实例
        return new OnlineMusicAdapter.PigViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        OnlineMusic onlineMusic = onlineMusics.get(position);
        //holder是RecyclerView.ViewHolder类型，是父类，不能直接获取到子类PigViewHolder的组件imageView...
        final OnlineMusicAdapter.PigViewHolder pigViewHolder = (OnlineMusicAdapter.PigViewHolder) holder;

        //渲染各组件
        pigViewHolder.song_name.setText((String) onlineMusic.getName());
        pigViewHolder.song_artist.setText((String) onlineMusic.getArtist());

        // 加载图片
        Picasso.get().load(onlineMusic.getPicUrl()).into(pigViewHolder.imageView);

        MusicItemClick clickListener = new MusicItemClick(onlineMusic);

        pigViewHolder.imageView.setOnClickListener(clickListener);
    }

    @Override
    public int getItemCount() {
        return onlineMusics.size();
    }

    class PigViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView song_name;
        TextView song_artist;

        PigViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.img);
            song_name = itemView.findViewById(R.id.song_name);
            song_artist = itemView.findViewById(R.id.song_artist);
        }
    }

    class MusicItemClick implements View.OnClickListener {
        OnlineMusic onlineMusic;

        MusicItemClick(OnlineMusic onlineMusic) {
            this.onlineMusic = onlineMusic;
        }

        @Override
        public void onClick(View v) {
            // 替换当前播放列表
            if (!MainApp.getInstance().getCurrentListName().equals(topMusicListName))
                new UpdateCurrentListThread().start();

            // 设置当前播放的音乐
            MainApp.getInstance().setCurrentMusic(new Music(
                    0,
                    onlineMusic.getName(),
                    onlineMusic.getPicUrl(),
                    onlineMusic.getArtist()
            ));

            // 设置当前列表名称
            MainApp.getInstance().setCurrentListName(topMusicListName);

            Intent intent = new Intent(v.getContext(), NowPlayingActivity.class);   // 跳转到播放页面
            context.startActivity(intent);
        }
    }

    /**
     * 异步更新当前播放列表
     */
    class UpdateCurrentListThread extends Thread {
        @Override
        public void run() {
            super.run();
            ArrayList<Music> playlist = MainApp.getInstance().getPlaylist();
            playlist.clear();
            int musicIndex = 0;
            for (OnlineMusic onlineMusic : onlineMusics) {
                playlist.add(new Music(
                        musicIndex++,
                        onlineMusic.getName(),
                        onlineMusic.getPicUrl(),
                        onlineMusic.getArtist()
                ));
            }
        }
    }
}
